<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="工作流名称" prop="workflowName">
        <el-input
          v-model="queryParams.workflowName"
          placeholder="请输入工作流名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工作流状态" prop="workflowStatus">
        <el-select v-model="queryParams.workflowStatus" placeholder="请选择工作流状态" clearable>
          <el-option
            v-for="dict in dict.type.pm_workflow_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:bid_project_workflow:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:bid_project_workflow:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:bid_project_workflow:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:bid_project_workflow:export']"
        >导出</el-button> 
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bid_project_workflowList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工作流名称" align="left" header-align="center" prop="workflowName" width="180"  />
      <el-table-column label="工作流节点" align="center">
        <template slot-scope="scope">
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item class="myColor" v-for="(item,index) in scope.row.bidProjectWorkflowPointList" :key="index" >
              {{item.pointName}}
            </el-breadcrumb-item>
          </el-breadcrumb>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="workflowStatus" width="85" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pm_workflow_status" :value="scope.row.workflowStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:bid_project_workflow:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:bid_project_workflow:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改投标进程管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="工作流名称" prop="workflowName">
          <el-input v-model="form.workflowName" placeholder="请输入工作流名称" />
        </el-form-item>
        <el-form-item label="工作流状态">
          <el-radio-group v-model="form.workflowStatus">
            <el-radio
              v-for="dict in dict.type.pm_workflow_status"
              :key="dict.value"
:label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>

          <span style="color:red;font-size:8px;">
            &nbsp;&nbsp;&nbsp;&nbsp;
            <i class="el-icon-question"></i>
            全局只能有一个启用的工作流
          </span>

        </el-form-item>
        <el-divider content-position="center">投标流程管理信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddBidProjectWorkflowPoint">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteBidProjectWorkflowPoint">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="bidProjectWorkflowPointList" :row-class-name="rowBidProjectWorkflowPointIndex" @selection-change="handleBidProjectWorkflowPointSelectionChange" ref="bidProjectWorkflowPoint">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="" align="center" prop="index" width="50"/>
          <el-table-column label="节点名称" prop="pointName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.pointName" placeholder="请输入节点名称" />
            </template>
          </el-table-column>
          <el-table-column label="顺序号" prop="pointOrder" width="150">
            <template slot-scope="scope">
              <el-input type="number" v-model="scope.row.pointOrder" placeholder="请输入顺序号" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBid_project_workflow, getBid_project_workflow, delBid_project_workflow, addBid_project_workflow, updateBid_project_workflow } from "@/api/business/bid_project_workflow";

export default {
  name: "Bid_project_workflow",
  dicts: ['pm_workflow_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedBidProjectWorkflowPoint: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 投标进程管理表格数据
      bid_project_workflowList: [],
      // 投标流程管理表格数据
      bidProjectWorkflowPointList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workflowName: null,
        workflowStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workflowName: [
          { required: true, message: "工作流名称不能为空", trigger: "blur" }
        ],
        workflowStatus: [
          { required: true, message: "工作流状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询投标进程管理列表 */
    getList() {
      this.loading = true;
      listBid_project_workflow(this.queryParams).then(response => {
        this.bid_project_workflowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        workflowId: null,
        workflowName: null,
        workflowStatus: "0"
      };
      this.bidProjectWorkflowPointList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.workflowId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加投标进程管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const workflowId = row.workflowId || this.ids
      getBid_project_workflow(workflowId).then(response => {
        this.form = response.data;
        this.bidProjectWorkflowPointList = response.data.bidProjectWorkflowPointList;
        this.open = true;
        this.title = "修改投标进程管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.bidProjectWorkflowPointList = this.bidProjectWorkflowPointList;
          if (this.form.workflowId != null) {
            updateBid_project_workflow(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBid_project_workflow(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const workflowIds = row.workflowId || this.ids;
      var deleteTitle = row.workflowName? ('"'+row.workflowName+'"'):'';
      this.$modal.confirm('是否确认删除' + deleteTitle + '？').then(function() {
        return delBid_project_workflow(workflowIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 投标流程管理序号 */
    rowBidProjectWorkflowPointIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 投标流程管理添加按钮操作 */
    handleAddBidProjectWorkflowPoint() {
      let obj = {};
      obj.pointName = "";
      obj.pointOrder = "";
      this.bidProjectWorkflowPointList.push(obj);
    },
    /** 投标流程管理删除按钮操作 */
    handleDeleteBidProjectWorkflowPoint() {
      if (this.checkedBidProjectWorkflowPoint.length == 0) {
        this.$modal.msgError("请先选择要删除的投标流程管理数据");
      } else {
        const bidProjectWorkflowPointList = this.bidProjectWorkflowPointList;
        const checkedBidProjectWorkflowPoint = this.checkedBidProjectWorkflowPoint;
        this.bidProjectWorkflowPointList = bidProjectWorkflowPointList.filter(function(item) {
          return checkedBidProjectWorkflowPoint.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleBidProjectWorkflowPointSelectionChange(selection) {
      this.checkedBidProjectWorkflowPoint = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/bid_project_workflow/export', {
        ...this.queryParams
      }, `bid_project_workflow_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.myColor >>> .el-breadcrumb__inner{

    background-color: #e8f4ff;
    border-color: #d1e9ff;
    display: inline-block;
    height: 32px;
    padding: 0 10px;
    line-height: 30px;
    font-size: 12px;
    color: #1890ff;
    border-width: 1px;
    border-style: solid;
    border-radius: 4px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    white-space: nowrap;
}
</style>
